/*
* @Author: majingyu
* @Date:   2017-04-26 10:23:36
* @Last Modified by:   jingyu.ma
* @Last Modified time: 2017-08-09 20:52:51
*/

import React from 'react';
import { connect } from 'react-redux';
import { Router, Route, Link, hashHistory } from 'react-router';
import RequireList from 'components/requireList.jsx';
import 'css/main.less';
import { changeNowPageWithHeaderState, getCity} from 'action/app';
import { eidtServiceInfoAction, changePageType,addTips,searchAllServiceInfoAction,editDemandId } from 'action/service';
import { changeCommentId } from 'action/commont.js';
import { local } from 'common/local.js';
import { getCookie,setCookie } from 'common/storage';
import { getQueryString } from 'common/getParams'

const mainData = (state) => {
    return {
        app:state.app,
        services: state.services
    }
}
class Main extends  React.Component{
    constructor(props){
        super(props);
        //获取当前页面的URL,然后解析openId 保存到cookie
        let openId = getQueryString(window.location.search, 'openId');
        setCookie('openId',openId);

        let localStation = local();
        this.state = {
            lat: localStation.lat,
            lng: localStation.lng
        };
        let data = {
            pageNumber:'1',
            pageSize: '10',
            distance:15,
            lat: this.state.lat,
            lng: this.state.lng
        }
        let params = {
            count:9
        }

        this.getMyCity();
        this.props.dispatch(searchAllServiceInfoAction(params));
        this.props.dispatch(eidtServiceInfoAction(data));
    }
    getMyCity(){
        let data = {
            lat:this.state.lat,
            lng:this.state.lng
        }
        this.props.dispatch(getCity(data));
    }
    getMoreList(){
        let data = {
            pageNumber:this.props.services.nearListList.pageNo+1,
            pageSize: '10',
            lat:this.state.lat,
            lng:this.state.lng,
            distance:15
        }
        this.props.dispatch(eidtServiceInfoAction(data));
    }
    goPage(){
        window.location.href = '#/searchList';
        this.props.dispatch(changeNowPageWithHeaderState('searchList'));
    }
    goDetails(status){
        if(status == 1){
            window.location.href = '#/service';
        }else{
            window.location.href = '#/skills';
        }
    }
    goDemandList(data){
        let params ={
            id: data.id,
            name: data.name
        }
        console.log('++++')
        console.log(params);
        console.log('++++')
        this.props.dispatch(editDemandId(params));
        window.location.href = '#/service';
    }
    commontList(id){
        this.props.dispatch(changeCommentId(id));
        this.props.dispatch(stayType())
        window.location.href = '#/demandOrSkill';
    }
    goToNotice(){
        window.location.href = '#/notice';
    }
    changeCity(){
        window.location.href = '#/city';
    }
    clickTips(id){
        let params = {
            id: id
        }
        this.props.dispatch(addTips(params));
    }
    render(){
        let seviceList = this.props.services.servicesCalssifyList.list.map((services)=>{
            return(
                <div className="sevice-info" key={services.id} onClick={() => {this.goDemandList(services)}}>
                    <div className="categeries " >
                        <img src="/image/require.png" alt="课目分类" />
                        <p className="text">{services.name}</p>
                    </div>
                </div>
            );
        });
        return(
            <div className="content">
                <div className="headImg">
                    {/*<img src="/image/city.png" alt="修改城市" className="city-info img-info" onClick={()=>{this.changeCity()}} />*/}
                    <img src="/image/notice.png" alt="通知" className="notice-block img-info" onClick={()=>{this.goToNotice()}} />
                    <img src="/image/banners.jpeg" alt="banner图" className="headImage"/>
                </div>
                <div className="our-categery">
                    {seviceList}
                    <Link to={{ pathname: '/sevicesList', query: { name:'main'} }}>
                        <div className="sevice-info" key='0'>
                            <div className="categeries ">
                                <img src="/image/require.png" alt="课目分类" />
                                <p className="text">全部</p>
                            </div>
                        </div>
                    </Link>
                </div>
                <div className="middle-info">
                    <div className="demandPool pool" onClick={()=>{this.goDetails(1)}}>
                        <img src="/image/requier.png" alt="需求池" />
                        <p className="text">需求池</p>
                    </div>
                    <div className="courierPool pool" onClick={()=>{this.goDetails(2)}}>
                        <img src="/image/skill.png" alt="技能池" />
                        <p className="text">技能池</p>
                    </div>
                </div>
                <RequireList servicesLists = {this.props.services.nearListList} commontList={(id) => {this.commontList(id)}} clickTips={(id) => {this.clickTips(id)}} cla="main-require" getMoreList={() => {this.getMoreList()}}/>
            </div>
            
        );
    }
}

let visibileMain = connect(mainData)(Main);

export default visibileMain;